//
//  Tool.swift
//  Food
//
//  Created by 罗林轩 on 2023/12/16.
//

import Foundation
import CoreData
import UIKit
struct Tool {
    /// 添加数据
    func saveData(breakfirst : [String],lunch:[String],dinner:[String]) {
        //获取管理的数据上下文 对象
        let app = UIApplication.shared.delegate as! AppDelegate
        let context = app.persistentContainer.viewContext
        
        //创建User对象
        let eat = NSEntityDescription.insertNewObject(forEntityName: "Eat",
                                                      into: context) as! Eat
        let nsbreakfirst = NSArray(array: breakfirst)
        let nsLunch = NSArray(array: lunch)
        let nsDinner = NSArray(array: dinner)
        
        eat.breakfirst = nsbreakfirst
        eat.lunch = nsLunch
        eat.dinner = nsDinner
        
        //保存
        do {
            try context.save()
            print("保存成功！")
        } catch {
            fatalError("不能保存：\(error)")
        }
    }
    
    /// 查询数据
    func queryData()->[Eat]
    {
        var arr : Array<Eat> = Array()
        //获取管理的数据上下文 对象
        let app = UIApplication.shared.delegate as! AppDelegate
        let context = app.persistentContainer.viewContext
        
        //声明数据的请求
        let fetchRequest = NSFetchRequest<Eat>(entityName:"Eat")
        fetchRequest.fetchLimit = 10 //限定查询结果的数量
        fetchRequest.fetchOffset = 0 //查询的偏移量
        
        //设置查询条件
//        let predicate = NSPredicate(format: "id= '1' ", "")
//        fetchRequest.predicate = predicate
        
        //查询操作
        do {
            let fetchedObjects = try context.fetch(fetchRequest)
            
            //遍历查询的结果
            for info in fetchedObjects{
                arr.append(info)
            }
            return arr
        }
        catch {
            fatalError("不能保存：\(error)")
        }
    }
    
    /// 修改数据操作
    func modifyData()
    {
        //获取管理的数据上下文 对象
        let app = UIApplication.shared.delegate as! AppDelegate
        let context = app.persistentContainer.viewContext
        
        //声明数据的请求
        let fetchRequest = NSFetchRequest<Eat>(entityName:"FoodModel")
        fetchRequest.fetchLimit = 10 //限定查询结果的数量
        fetchRequest.fetchOffset = 0 //查询的偏移量
        
        //设置查询条件
        let predicate = NSPredicate(format: "id= '1' ", "")
        fetchRequest.predicate = predicate
        
        //查询操作
        do {
            let fetchedObjects = try context.fetch(fetchRequest)
            
            //遍历查询的结果
            for info in fetchedObjects{
                //修改密码
                //                info.password = "abcd"
                //重新保存
                try context.save()
            }
        }
        catch {
            fatalError("不能保存：\(error)")
        }
    }
    
    /// 删除数据操作
    func deleteData()
    {
        //获取管理的数据上下文 对象
        let app = UIApplication.shared.delegate as! AppDelegate
        let context = app.persistentContainer.viewContext
        
        //声明数据的请求
        let fetchRequest = NSFetchRequest<Eat>(entityName:"FoodModel")
        fetchRequest.fetchLimit = 10 //限定查询结果的数量
        fetchRequest.fetchOffset = 0 //查询的偏移量
        
        //设置查询条件
        let predicate = NSPredicate(format: "id= '1' ", "")
        fetchRequest.predicate = predicate
        
        //查询操作
        do {
            let fetchedObjects = try context.fetch(fetchRequest)
            
            //遍历查询的结果
            for info in fetchedObjects{
                //删除对象
                context.delete(info)
            }
            
            //重新保存-更新到数据库
            try! context.save()
        }
        catch {
            fatalError("不能保存：\(error)")
        }
    }
    
}
